package listAdapterHistory.p3_fitnessapp;

import java.util.List;

import model.p3_fitnessapp.History;
import model.p3_fitnessapp.MySQLiteHelper;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import controler.p3_fitnessapp.R;
import controler.p3_fitnessapp.R.id;

public class HistoryDetails extends Activity {

	/*
	 * ==========================================================================
	 * Globale Variablen
	 * ========================================================
	 */
	private List<HistoryList> itemList = null;
	private List<History> itemListHistory = null;
	private ListView listView = null;
	private String date = " ";

	/*
	 * ==========================================================================
	 * Android Methoden ========================================================
	 */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Layout aufrufen ( ListView )
		setContentView(R.layout.uact_history_details);

	}

	@Override
	protected void onResume() {

		MySQLiteHelper dbHistoryList = new MySQLiteHelper(
				getApplicationContext());

		itemListHistory = dbHistoryList.getAllHistorys();

		// FIXME History Daten aus Objekt in Liste schreiben
		// for (History history : itemListHistory) {
		// Log.d(null, "FOR History");
		//
		// Log.e(null, "History Date " + history.getDate());
		//
		// if (date.equals(history.getDate().toString())) {
		// Log.d(null, "Date korrekt ");
		//
		// Log.d(null, "Date If  : " + history.getDate().toString());
		// Log.d(null, "Date If java  : " + date);
		//
		// HistoryList itemHistory = new HistoryList();
		//
		// itemHistory.setExercise(history.getName_exercise());
		// itemHistory.setSet1kg(history.getKilograms());
		//
		// // itemList.add(itemHistory);
		//
		// } else {
		// Log.d(null, "Date ungleich ");
		//
		// date = "" + history.getDate();
		// Log.d(null, "new : " + date);
		//
		// Log.d(null, "Date Else : " + history.getDate());
		// Log.d(null, "Date else java  : " + date);
		//
		// HistoryList itemHistory = new HistoryList();
		//
		// itemHistory.setExercise(history.getName_exercise());
		// itemHistory.setMusclegroup("Test");
		//
		// Log.d(null, "itemHistory " + itemHistory.getExercise());
		//
		// // itemList.add(itemHistory);
		//
		// }
		// Log.d(null, "endif");

		// }

		// itemList = getItemList();

		// for (HistoryList hist : itemList){
		// Log.e(null, "FOR");
		// hist.getMusclegroup();
		// Log.d(null, "items in List : " + hist.getMusclegroup());
		// }

		super.onResume();

		ListView listView = (ListView) findViewById(R.id.lv_history_details);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		listView.setAdapter(new myHistoryAdapter());

	}

	/*
	 * ==========================================================================
	 * Benutzer Interaktion (Buttons ... )
	 * ========================================================
	 */
	public void onButtonClick(View view){
		
		switch (view.getId()) {
		case R.id.btn_okay:
			Log.d(null, "OK Pressed");
			onBackPressed();
			break;

		default:
			break;
		}
		
	}
	/*
	 * ==========================================================================
	 * Eigene Methoden ========================================================
	 */

	/*
	 * ==========================================================================
	 * Interne Klassen ========================================================
	 */

	private class myHistoryAdapter extends ArrayAdapter<History> {

		public myHistoryAdapter() {
			super(getApplicationContext(), R.layout.uact_history_details_list,
					itemListHistory);

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			View row = convertView;

			if (row == null) {

				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.uact_history_details_list,
						parent, false);

			}

			TextView dateTV = (TextView) row.findViewById(R.id.tv_date_details);
			TextView exercise = (TextView) row
					.findViewById(R.id.tv_details_exercise);
			TextView set1kg = (TextView) row.findViewById(R.id.tv_set1kg);
			TextView set1wdh = (TextView) row.findViewById(R.id.tv_set1wdh);

			TextView set = (TextView) row.findViewById(id.tv_set);

			for (History history : itemListHistory) {

				
				// FIXME Datum soll bei doppeltem Eintrag nicht in TV geschrieben werden
//				if (!date.equals(history.getDate())) {

					exercise.setText(itemListHistory.get(position)
							.getName_exercise());
					dateTV.setText((CharSequence) itemListHistory.get(position)
							.getDate());
					set1kg.setText(String.valueOf(itemListHistory.get(position)
							.getKilograms()));
					set1wdh.setText(String.valueOf(itemListHistory
							.get(position).getRepetition()));
					set.setText("Satz "
							+ itemListHistory.get(position).getSet());
//					date = "" + history.getDate();

//				} else {
//					exercise.setText(itemListHistory.get(position)
//							.getName_exercise());
//					dateTV.setText((CharSequence) itemListHistory.get(position)
//							.getDate());
//					dateTV.setText((CharSequence) itemListHistory.get(position).getDate());
//					set1kg.setText(String.valueOf(itemListHistory.get(position)
//							.getKilograms()));
//					set1wdh.setText(String.valueOf(itemListHistory
//							.get(position).getRepetition()));
//					set.setText("Satz "
//							+ itemListHistory.get(position).getSet());
//
//				}

			}

			return (row);

		}

	}

}
